package com.cscp.repository;


import com.cscp.entity.Consumer;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import javax.transaction.Transactional;
import java.util.List;

@Repository
public interface CustomerRepository extends PagingAndSortingRepository<Consumer, Long> {

    @Query("FROM Consumer WHERE customerName=:customerName")
    List<Consumer> findByCustomerName(@Param("customerName") String customerName);

    @Transactional
    @Modifying
    @Query("UPDATE Consumer c SET c.customerName=:customerName WHERE c.customerId=:customerId")
    int updateByCustomerId(@Param("customerName") String customerName, @Param("customerId") Long customerId);

    @Transactional
    @Modifying
    @Query("DELETE FROM Consumer c WHERE c.customerId=?1")
    int deleteByCustomerId(Long customerId);

    /**
     * JQL不支持insert
     * @param customerId
     * @return
     */
//    @Transactional
//    @Modifying
//    @Query("INSERT INTO Consumer (customerName) SELECT c.customerName FROM Consumer c WHERE c.customerId=?1")
//    int insertByCustomerId(Long customerId);

    @Query(value = "select * FROM t_consumer WHERE customer_name=:customerName", nativeQuery = true)
    List<Consumer> findNativeByCustomerName(@Param("customerName") String customerName);

}
